package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISignUpDAO;
import org.jsola.hr.dto.SignUpAddDTO;
import org.jsola.hr.dto.SignUpListUpdateDTO;
import org.jsola.hr.dto.SignUpUpdateDTO;
import org.jsola.hr.entity.SignUpDO;
import org.jsola.hr.query.SignUpQuery;
import org.jsola.hr.service.ISignUpService;
import org.jsola.hr.vo.SignUpListVO;
import org.jsola.hr.vo.SignUpVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.HtmlUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公司首页报名表
 *
 * @author zhr
 */
@Service("hrSignUpServiceImpl")
public class SignUpServiceImpl implements ISignUpService {

    @Autowired
    private ISignUpDAO signUpDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SignUpVO save(SignUpAddDTO signUpAddDTO, TokenUser tokenUser) {
        // 转DO
        SignUpDO signUpDO = signUpAddDTO.to(SignUpDO.class);
        // 保存
        signUpDO = save(signUpDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return signUpDO.to(SignUpVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SignUpUpdateDTO signUpUpdateDTO, TokenUser tokenUser) {
        //转DO
        SignUpDO signUpDO = signUpUpdateDTO.to(SignUpDO.class);
        //防止出现转码问题
        signUpUpdateDTO.setImgUrl(HtmlUtils.htmlUnescape(signUpUpdateDTO.getImgUrl()));
        signUpUpdateDTO.setLink(HtmlUtils.htmlUnescape(signUpUpdateDTO.getLink()));
        //根据主键更新，只更新非null值
        return updateByIdSelective(signUpDO, signUpDO.getSiteId(), tokenUser.getUserId());
    }

    @Override
    public int updateList(SignUpListUpdateDTO signUpListUpdateDTO, TokenUser tokenUser) {
        int num = 0;
        if (CollectionUtils.isEmpty(signUpListUpdateDTO.getSignUpUpdateDTOList())){
            throw new ParamException("修改内容不能为空");
        }
        for (SignUpUpdateDTO signUpUpdateDTO : signUpListUpdateDTO.getSignUpUpdateDTOList()){
            num += update(signUpUpdateDTO,tokenUser);
        }
        return num;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... signUpIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) signUpIds);
    }


    @Override
    public SignUpVO selectById(String signUpId, String siteId) {
        SignUpDO signUpDO = selectDOById(signUpId, siteId);
        if (signUpDO == null) {
            return null;
        }
        return signUpDO.to(SignUpVO.class);
    }

    @Override
    public List<SignUpListVO> select(SignUpQuery signUpQuery, String siteId) {
        List<SignUpDO> signUpDOList = selectDO(signUpQuery, siteId);
        if(CollectionUtils.isEmpty(signUpDOList)) {
            return signUpDOList == null ? null : new ArrayList<>();
        }
        return signUpDOList.stream()
                .map(signUpDO -> signUpDO.to(SignUpListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SignUpQuery signUpQuery, String siteId) {
        Example example = buildExample(signUpQuery,siteId);
        return signUpDAO.selectCountByExample(example);
    }

    @Override
    public Page<SignUpListVO> selectPage(SignUpQuery signUpQuery, String siteId) {
        Example example = buildExample(signUpQuery,siteId);
        Page<SignUpDO> page = signUpDAO.selectPageByExample(example,
                signUpQuery.getPageNo(),
                signUpQuery.getPageSize());

        return page.to(SignUpListVO.class);
    }

    @Override
    public SignUpDO selectDOById(String signUpId, String siteId) {
        return listById(signUpId, siteId);
    }

    @Override
    public List<SignUpDO> selectDO(SignUpQuery signUpQuery, String siteId) {
        Example example = buildExample(signUpQuery,siteId);
        return signUpDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param signUpQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SignUpQuery signUpQuery, String siteId) {
        Example example = new Example(SignUpDO.class);
        example.and()
                .andEqualTo("valid", true);
        // 排序
        ExampleKit.setExampleOrder(example,signUpQuery.getOrders());
        return example;
    }
}




